package com.example.auth.service.impl;

import com.example.auth.dto.LoginRequest;
import com.example.auth.dto.LoginResponse;
import com.example.auth.dto.UserInfo;
import com.example.auth.service.AuthService;
import com.example.auth.service.JwtService;
import lombok.RequiredArgsConstructor;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class AuthServiceImpl implements AuthService {

    private final AuthenticationManager authenticationManager;
    private final JwtService jwtService;

    @Override
    public LoginResponse login(LoginRequest request) {
        Authentication authentication = authenticationManager.authenticate(
            new UsernamePasswordAuthenticationToken(
                request.getUsername(),
                request.getPassword()
            )
        );

        String token = jwtService.generateToken(authentication);
        
        UserInfo userInfo = UserInfo.builder()
            .id(1L) // 这里应该从认证用户中获取
            .username(request.getUsername())
            .email(request.getUsername() + "@example.com")
            .role("USER")
            .build();

        return LoginResponse.builder()
            .token(token)
            .userInfo(userInfo)
            .build();
    }
} 